/*
 * @Author: 余文童
 * @Date: 2020-08-10 11:59:41
 * @LastEditTime: 2020-08-13 14:39:49
 * @LastEditors: Please set LastEditors
 * @Description: 订单控制器
 * @FilePath: \BackEnd\saleflower\src\main\java\com\wangwang\saleflower\controller\OrderController.java
 */
package com.wangwang.saleflower.controller;

import java.util.Date;
import java.sql.Timestamp;
import java.util.Map;

import com.wangwang.saleflower.entity.Order;
import com.wangwang.saleflower.entity.ResultState;
import com.wangwang.saleflower.entity.ResultState.State;
import com.wangwang.saleflower.service.FlowerService;
import com.wangwang.saleflower.service.OrderService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@RestController
@RequestMapping("/api")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private FlowerService flowerService;

    @GetMapping(value = "/orders")
    @ResponseBody
    public ResultState getAllOrders(@RequestParam(name = "usernum") int userNum) {
        ResultState result;
        var orders = orderService.selectOrderAndProduct(userNum);
        if (!orders.isEmpty()) {
            result = ResultState.CreateResult(State.SUCCESS, orders, "成功获取");
        } else {
            result = ResultState.CreateResult(State.ERROR, null, "不存在订单");
        }
        return result;
    }

    @PostMapping(value = "/add/order")
    @ResponseBody
    public ResultState addOrder(@RequestBody Map<String, Object> val) {
        ResultState result;
        var fid = Integer.parseInt(val.get("fwnum").toString());
        var flower = flowerService.selectFlowerList(fid);
        if (flower == null) {
            result = ResultState.CreateResult(State.ERROR, null, "该商品不存在");
        } else {
            Order order = new Order();
            order.setFwnum(fid);
            order.setQuantity((long) 1);
            Date date = new Date();
            Timestamp t = new Timestamp(date.getTime());
            order.setTime(t);
            Integer userNum = (Integer)val.get("usernum");
            order.setUsernum(userNum.longValue());
            order.setTotal(flower.getPrice());
            order.setAddress(flower.getOrigin());
            orderService.addOrder(order);
            result = ResultState.CreateResult(State.SUCCESS, null, "成功添加");
        }
        return result;
    }

    @DeleteMapping(value = "/delete/orders")
    @ResponseBody
    public ResultState removeOrders(@RequestBody Long[] ids) {
        ResultState result;
        boolean tmp = true;
        for (var orderId : ids) {
            tmp = orderService.isOrderExist(orderId) && tmp;
        }
        if (tmp) {
            orderService.removeOrders(ids);
            result = ResultState.CreateResult(State.SUCCESS, null, "删除成功");
        } else {
            result = ResultState.CreateResult(State.ERROR, null, "含有不存在的订单");
        }
        return result;
    }

}